<?php 

class RecipeCategoriesController extends AppController{
	
	//public $scaffold;
	public $name = 'RecipeCategories';
	public $layout = 'admin';

	
	public function admin_index()
	{
		$this->set(array('title_for_layout' =>  'All recipes categories'));
		$this->RecipeCategory->recursive = 0;
		$this->paginate = array('order' => array('title' => 'ASC'));
		$this->set('recipecategory', $this->paginate());

	}

	public function admin_add()
	{
		$this->set(array('title_for_layout' => __('Add new recipe category')));

		if($this->request->is('post') || $this->request->is('put'))
		{
			$this->RecipeCategory->create();
			if($this->RecipeCategory->save($this->request->data))
			{
				$this->Session->setFlash('The recipe category has been saved');
				$this->redirect(array('controller' => 'recipecategories', 'action' => 'index'));
			}
			else
			{
				$this->Session->setFlash('The recipe category has not been saved, please try again');
			}

		}
		else
		{
			$listcategory = $this->RecipeCategory->find('all', array('recursive' => -1, 'fields' => array('id', 'title'), 'order' => array('title' => 'ASC'),));
			$this->set('listcategory', $listcategory);

		}

		

	}

	public function admin_edit($id = null)
	{
		$this->set(array('title_for_layout' => 'Edit the recipe category'));

		if(!$id)
		{
			$this->Session->setFlash('Please provide a recipe category id');
			$this->redirect(array('controller' => 'recipecategories', 'action' => 'index'));
		}

		$recipecategory = $this->RecipeCategory->findById($id);

		if(!$recipecategory)
		{
			$this->Session->setFlash('Please provide a valid recipe category id');
			$this->redirect(array('controller' => 'recipecategories', 'action' => 'index'));
		}

		$this->RecipeCategory->id = $id;

		if($this->request->is('post') || $this->request->is('put'))
		{
			

			if($this->RecipeCategory->save($this->request->data))
			{
				$this->Session->setFlash('The recipe category has been updated');
				$this->redirect(array('controller' => 'recipecategories', 'action' => 'index'));
			}
			else
			{
				$this->Session->setFlash('The recipe category has not been saved, please try again');
			}
		}
	
		$this->RecipeCategory->recursive = -1;
		$this->request->data = $this->RecipeCategory->read();

		$listcategory = $this->RecipeCategory->find('all', array('recursive' => -1, 'fields' => array('id', 'title'), 'order' => array('title' => 'ASC'),));
		$this->set('listcategory', $listcategory);	

	}

	public function admin_delete($id = null)
	{
		$this->set(array('title_for_layout' => 'Delete the recipe category'));

		if(!$id)
		{
			$this->Session->setFlash('Please provide a recipe category id');
			$this->redirect(array('controller' => 'recipecategories', 'action' => 'index'));
		}

		$recipecategory = $this->RecipeCategory->findById($id);

		if(!$recipecategory)
		{
			$this->Session->setFlash('Please provide a valid recipe category id');
			$this->redirect(array('controller' => 'recipecategories', 'action' => 'index'));
		}

		$this->RecipeCategory->id = $id;

		if($this->RecipeCategory->delete())
		{
			$this->Session->setFlash('The recipe category has been deleted');
			$this->redirect(array('controller' => 'recipecategories', 'action' => 'index'));
		}
		else
		{
			$this->Session->setFlash('The recipe category has not been deleted, please try again');
			$this->redirect(array('controller' => 'recipecategories', 'action' => 'index'));
		}
	}

	
}

?>